//
//  HWGiftBagCenterDetailAlert.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/9/22.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGiftBagCenterDetailAlert: MMPopupView {
    private var tapBgView:UIView!
    private var bgView: UIView!
    var logoImageView:UIImageView!
    var nameLabel:UILabel!
    var cancelButton:UIButton!
    var tableView:UITableView!
    var isShow = false
    var tableViewProtocol = HWGiftBagCenterDetailProtocol()
    var model:HWGiftBagCenterDetailModel? {
        didSet {
            logoImageView.kf.setImage(with: URL(string: model?.icon ?? ""))
            nameLabel.text = model?.name
            tableViewProtocol.model = model
            tableView.reloadData()
        }
    }
    @objc var close:(()->())?
    var copyButtonClick:((_:String)->())?
    var receiveButtonClick:((_:String)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
        addAction()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func show() {
        super.show()
        isShow = true
    }
    
    override func hide() {
        super.hide()
        isShow = false
    }

}

extension HWGiftBagCenterDetailAlert {
    private func initUI() -> Void {
        type = MMPopupType.sheet
        backgroundColor = UIColor.clear
        attachedView = UIApplication.shared.windows.filter({ $0.isKeyWindow }).last?.rootViewController?.view
        
        tapBgView = UIView.init()
        tapBgView.backgroundColor = UIColor.clear
        addSubview(tapBgView)
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(bacgroundTap(_:)))
        tapBgView.addGestureRecognizer(tap)
        
        bgView = UIView()
        bgView.backgroundColor = .white
        bgView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 500*widthScale)
        addSubview(bgView)
        bgView.addCorner(conrners: [.topLeft,.topRight], radius: 18*widthScale)
        
        cancelButton = UIButton(type: .custom)
        cancelButton.setTitle("关闭", for: .normal)
        cancelButton.addTarget(self, action: #selector(cancelButtonClick(_:)), for: .touchUpInside)
        cancelButton.setTitleColor(.colorWithHexString(hexString: "#888888"), for: .normal)
        cancelButton.titleLabel?.font = .boldSystemFont(ofSize: 13*widthScale)
        cancelButton.layer.cornerRadius = 14*widthScale
        bgView.addSubview(cancelButton)
        
        logoImageView = UIImageView()
        logoImageView.layer.cornerRadius = 10*widthScale
        logoImageView.layer.masksToBounds = true
        bgView.addSubview(logoImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 16*widthScale)
        bgView.addSubview(nameLabel)
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .colorWithHexString(hexString: "#FFFFFF")
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        bgView.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -40
        if #available(iOS 15.0, *) {
            self.tableView.sectionHeaderTopPadding = 0
        } else {
            // Fallback on earlier versions
        }
        
    }
    
    private func initFrame() -> Void {
        snp.makeConstraints { make in
            make.width.equalTo(screenWidth)
            make.height.equalTo(screenHeight)
        }
        
        tapBgView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(0)
        }
        
        bgView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(500*widthScale)
        }
        
        cancelButton.snp.makeConstraints { make in
            make.top.equalTo(21*widthScale)
            make.right.equalTo(-18*widthScale)
            make.height.equalTo(18*widthScale)
        }
        
        logoImageView.snp.makeConstraints { make in
            make.left.top.equalTo(18*widthScale)
            make.width.height.equalTo(25*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(logoImageView)
            make.left.equalTo(logoImageView.snp.right).offset(5*widthScale)
            make.height.equalTo(23*widthScale)
            make.right.equalTo(cancelButton.snp.left).offset(-18*widthScale)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(logoImageView.snp.bottom).offset(9*widthScale)
        }
        
//        logoImageView.setContentCompressionResistancePriority(.required, for: .horizontal)
        
    }
}
extension HWGiftBagCenterDetailAlert {
    @objc func bacgroundTap(_ tap:UITapGestureRecognizer) -> Void {
        hide()
    }
    
    @objc func cancelButtonClick(_ button:UIButton) -> Void {
        hide()
    }
    
    func addAction() -> Void {
        self.tableViewProtocol.copyButtonClick = {
            self.copyButtonClick?($0)
        }
        
        self.tableViewProtocol.receiveButtonClick = {
            self.receiveButtonClick?($0)
        }
    }
    
}

